<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>金额大小</title>
    <script src="jquery-1.11.3.min.js"></script>
    <style>
        .hidden{
          display: none
      }
    </style>
</head>

<body>
    <div class="inputBox">
        <input type="text" name="fAmount" class="htmlInputText amount" onkeyup="clearNoNum(this)">
        <span class="m-Text">人民币</span>
    </div>
    <div class="bigBox">
        <div class="">大写：</div>
        <div class="" id="uppercaseCurr"></div>
    </div>
    <span style="color:red;" class="hidden amountShow">※请填写0.01~9999999.99之间的数字！</span>
</body>
<script>
    $(function () {
        //金额转换
        function formatMoney(s, type) {
            if (/[^0-9\.]/.test(s))
                return "";
            if (s == null || s == "null" || s == "")
                return "";
            s = s.toString().replace(/^(\d*)$/, "$1.");
            s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
            s = s.replace(".", ",");
            var re = /(\d)(\d{3},)/;
            while (re.test(s))
                s = s.replace(re, "$1,$2");
            s = s.replace(/,(\d\d)$/, ".$1");
            if (type == 0) {
                var a = s.split(".");
                if (a[1] == "00") {
                    s = a[0];
                }
            }
            return s;
        }
        //失去焦点
        $(".amount").blur(function () {

            $(this).val(formatMoney($(this).val(), 2));

        });

        var data = {
            regular: function () {
                var money = /^(0|[1-9]\d{0,6})(\.\d{1,2})?$/;
                if (!money.test($(".amount").val()) || $(".amount").val() == 0) {
                    $(".amountShow").removeClass("hidden");
                    return 1;
                } else {
                    $(".amountShow").addClass("hidden");
                    return 0;
                }
            }
        };
        /** 数字金额大写转换(可以处理整数,小数,负数) */

        function smalltoBIG(n) {
            var htmlD = '';
            var fraction = ['角', '分'];
            var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
            var unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']];
            var head = n < 0 ? '欠' : '';
            n = Math.abs(n);

            var s = '';

            for (var i = 0; i < fraction.length; i++) {
                s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
            }
            s = s || '整';
            n = Math.floor(n);

            for (var i = 0; i < unit[0].length && n > 0; i++) {
                var p = '';
                for (var j = 0; j < unit[1].length && n > 0; j++) {
                    p = digit[n % 10] + unit[1][j] + p;
                    n = Math.floor(n / 10);
                }
                s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
            }
            return htmlD + head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
        }
        $(".amount").keyup(function () {
            data.regular();
            $("#uppercaseCurr").html(smalltoBIG($(".amount").val().replace(",", "")));
        });
    })

</script>

<script>
    function clearNoNum(obj) {
        obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = parseFloat(obj.value);
        }
    }
</script>

<script>
	
	 //格式化金额
	    function formatCurr (s){   
	 	   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";
	 	   var l = s.split(".")[0].split("").reverse(),   
	 	   r = s.split(".")[1];   
	 	   t = "";   
	 	   for(i = 0; i < l.length; i ++ )   
	 	   {   
	 	      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
	 	   }   
	 	   return '￥' + t.split("").reverse().join("") + "." + r;   
	    }
			var mo = '1000.055'
			console.log(formatCurr(mo))
</script>
</html>